package chapter10;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-20 下午4:58
 */
public class GcTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(GcTest.class);

    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        String str = "";
        for (int i = 0; i < 5000; i++) {
            str += i;
        }

        LOGGER.info("----------- Before GC -----------");
        LOGGER.info("maxMemory:{} MB", runtime.maxMemory() / 1024 / 1024.0);
        LOGGER.info("totalMemory:{} MB", runtime.totalMemory() / 1024 / 1024.0);
        LOGGER.info("freeMemory:{} MB", runtime.freeMemory() / 1024 / 1024.0);

        runtime.gc();

        LOGGER.info("----------- After GC -----------");
        LOGGER.info("maxMemory:{} MB", runtime.maxMemory() / 1024 / 1024.0);
        LOGGER.info("totalMemory:{} MB", runtime.totalMemory() / 1024 / 1024.0);
        LOGGER.info("freeMemory:{} MB", runtime.freeMemory() / 1024 / 1024.0);

        Process pro = null;
        try {
            pro = runtime.exec("gimp");
            LOGGER.info("start gimp!");
            Thread.sleep(10000);
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        } finally {
            if (null != pro) {
                LOGGER.info("stop gimp~");
                pro.destroy();
            }
        }
    }
}
